/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_API_REFLECTION_TYPE_SYSTEM_HPP
#define	_CUBEFS_CORE_API_REFLECTION_TYPE_SYSTEM_HPP

#include <cubefs/api/reflection/type.hpp>
#include <cubefs/api/exceptions/bounds.hpp>
#include <cubefs/api/reflection/type_system.hpp>

#define CUBEFS_SOURCE "cubefs/core/api/reflection/type_system.hpp:" CUBEFS_AT

#include <map>

namespace cubefs    {
namespace api       {

template <typename Interface>
class TypeSystemImplT: public TypedImplT<Interface> {
	typedef TypeSystemImplT<Interface> 			Me;
	typedef TypedImplT<Interface>				Base;

    typedef std::map<String, Type*> 			TypeMap;

    TypeMap types_;

public:
    virtual ~TypeSystemImplT() throw () {
        try {
            for (TypeMap::iterator i = types_.begin(); i != types_.end(); i++) {
                delete i->second;
            }
        }
        catch (...) {}
    }

    virtual void Register(Type* type)
    {
        types_[type->name()] = type;
    }

    virtual void Unregister(Type* type)
    {
        types_.erase(type->name());
    }

    virtual Type* Find(StringRef name) const {
        TypeMap::const_iterator i = types_.find(name);
        if (i != types_.end()) {
            return i->second;
        }
        else {
            return NULL;
        }
    }

    virtual ListOfTypes* Types() const {
        ListOfTypes* list = new ListOfTypesImpl();

        for (TypeMap::const_iterator i = types_.begin(); i != types_.end(); i++) {
            list->Add(i->second);
        }

        return list;
    }



    static void InitType()
    {
    	type_ = new AbstractType(TypeNameFactory<Interface>::name(), Typed::MyType());
    }

    static void InitOperations()
    {
    	AbstractType *type = static_cast<AbstractType*>(type_);
    	type->AddOperation(new BasicOperation<Typed* (Me::*)(), ListOfTypes* (Me::*)() const>(type, ListOfTypes::MyType(), "Types", &Me::Types));
    	type->AddOperation(new BasicOperation<Typed* (Me::*)(StringRef), Type* (Me::*)(StringRef) const>(type, Type::MyType(), "Find", "name", &Me::Find));

    	type->AddOperation(new BasicOperation<void (Me::*)(Type*)>(type, "Register", "type", &Me::Register));
    	type->AddOperation(new BasicOperation<void (Me::*)(Type*)>(type, "Unregister", "type", &Me::Unregister));
    }


    CUBEFS_DECLARE_REFLECTION;
};

template <typename Interface>
Type* TypeSystemImplT<Interface>::type_ 	= NULL;
typedef TypeSystemImplT<TypeSystem> 		TypeSystemImpl;

}}

#undef CUBEFS_SOURCE

#endif
